﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using System.Data;

namespace Catering.Catering_Page.richang
{
    public partial class rbzt : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TextBox1.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd'})");//日期控件初始化
                loadPage();
            }
        }

        /// <summary>
        /// 作业区、班组初始化
        /// </summary>
        private void loadPage()
        {
            //frRules = new FromRecords();
            if (Session["User"] != null)
            {
                string userid = Session["PersonnelID"].ToString();
                int companyid = 0;
                int groupid = 0;
                int classid = 0;
                bool isAdmin = false;
                Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);
                Bind_Data_to_Controls.Bind_Area_ddl(ddl_Area, isAdmin, groupid, companyid, true, false);
                Bind_Data_to_Controls.Bind_Class_ddl(ddl_Area.SelectedItem, ddl_class, isAdmin, classid, true);
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string areaID = "";                   //作业区
            string classID = "";                  //班组
            DateTime searchDate = new DateTime(); //搜索日期

            areaID = this.ddl_Area.SelectedItem.Value;               //作业区
            classID = this.ddl_class.SelectedItem.Value;             //班组
            DateTime.TryParse(this.TextBox1.Text, out searchDate);

            DataTable dt = new DataTable();
            dt.Columns.Add("CName", typeof(string));
            dt.Columns.Add("IsAddDynamic", typeof(string));
            dt.Columns.Add("AddPerson", typeof(string));

            string whereclause = "";
            if (searchDate.Year < 1949) return;
            if (classID != "" && classID != "-10") whereclause = " and ClassID= " + classID + " ";

            DataTable tempall = new Catering_Rules.DailyManagement().GetList("Date='" + searchDate.ToString() + "'" + whereclause).Tables[0];

            Dictionary<int, string> list = GetClassList(areaID, classID);

            if (list != null)
            {
                foreach (int id in list.Keys)
                {
                    DataRow temprow = dt.NewRow();
                    temprow["CName"] = list[id];
                    DataRow[] tempsel = tempall.Select(" ClassID=" + id + "");
                    if (tempsel != null && tempsel.Length > 0)
                    {
                        temprow["IsAddDynamic"] = "是";
                        int pid = Convert.ToInt32(tempsel[0]["RID"]);
                        if (pid > 0)
                        {
                            Catering_Model.E_Personnel md_person = new Catering_Rules.T_Personnel().GetModel(pid);
                            if (md_person != null) temprow["AddPerson"] = md_person.Name;
                        }
                    }
                    else
                    {
                        temprow["IsAddDynamic"] = "<span class='red'>否</span>";
                    }
                    dt.Rows.Add(temprow);
                }
            }

            this.Rept_Rbzt.DataSource = dt;//配餐在岗人员
            this.Rept_Rbzt.DataBind();
        }

        public Dictionary<int, string> GetClassList(string groupid, string classid)
        {
            Dictionary<int, string> result = new Dictionary<int, string>();
            if (!string.IsNullOrEmpty(groupid) && !groupid.Equals("-10"))
            {
                if (!string.IsNullOrEmpty(classid) && !classid.Equals("-10"))
                {
                    result.Add(Convert.ToInt32(classid), GetAllDDL_Class()[Convert.ToInt32(classid)]);
                    return result;
                }
                else
                {
                    return GetAllDDL_Class();
                }
            }
            else
            {
                Dictionary<int, string> temp = GetAllDDL_Area();
                List<Catering_Model.ClassInfo> listClass = new Catering_Rules.ClassInfo().GetAllList(0);
                if (temp.Count > 0)
                {
                    foreach (int id in temp.Keys)
                    {
                        foreach (Catering_Model.ClassInfo item in listClass.Where(c => c.AreaID == id))
                        {
                            result.Add(item.id, item.CName);
                        }
                    }
                }
            }
            return result;
        }


        public Dictionary<int, string> GetAllDDL_Area()
        {
            Dictionary<int, string> result = new Dictionary<int, string>();
            foreach (ListItem item in ddl_Area.Items)
            {
                if (!string.IsNullOrEmpty(item.Value) && !item.Value.Equals("-10"))
                {
                    result.Add(Convert.ToInt32(item.Value), item.Text);
                }
            }
            return result;
        }

        public Dictionary<int, string> GetAllDDL_Class()
        {
            Dictionary<int, string> result = new Dictionary<int, string>();
            foreach (ListItem item in ddl_class.Items)
            {
                if (!string.IsNullOrEmpty(item.Value) && !item.Value.Equals("-10"))
                {
                    result.Add(Convert.ToInt32(item.Value), item.Text);
                }
            }
            return result;
        }

        protected void ddl_Area_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl_Area.SelectedItem != null)
            {
                Bind_Data_to_Controls.Bind_Class_ddl(ddl_Area.SelectedItem, ddl_class);
            }
        }
    }
}